package com.cooldatasoft.util;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.net.SMTPAppender;

/**
 * 
 * @author fatih mehmet ucar
 *
 */
public class SMTPLogAppender extends SMTPAppender {
	
	/**
	 * @see org.apache.log4j.spi.OptionHandler#activateOptions()
	 */
	public void activateOptions() {
		Properties props = new Properties(System.getProperties());
		if (super.getSMTPHost() != null)
			props.put("mail.smtp.host", super.getSMTPHost());
		if (port != null) {
			props.put("mail.smtp.port", port);
		}

		Session session = null;
		if(authReuired){
			 // New authentication bit
			props.put("mail.smtp.auth", "true");
			Authenticator auth = new SMTPAuthenticator();
			session = Session.getInstance(props, auth);
		}else{
			session = Session.getInstance(props, null);	
		}
		
		// session.setDebug(true);
		msg = new MimeMessage(session);

		try {
			if (super.getFrom() != null){
				msg.setFrom( new InternetAddress(super.getFrom()) );
			}else{
				msg.setFrom();
			}

			msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(super.getTo(), true));
			
			
			if(super.getSubject() != null){
                msg.setSubject(getIpAddress()+" "+super.getSubject());
			}
			
		} catch (MessagingException e) {
			LogLog.error("Could not activate SMTPAppender options.", e);
		}

	}

	private String getIpAddress(){
		InetAddress inetAddress;
		try {
			inetAddress = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			LogLog.error("Cannot find ip address.",e);
			return "UNKNOWN";
		}
	    return inetAddress.getHostAddress();
	}
	 /**
	* SimpleAuthenticator is used to do simple authentication
	* when the SMTP server requires it. It pulls the username
	* and password from log4j.properties or where appropriate.
	*/
	private class SMTPAuthenticator extends javax.mail.Authenticator {

		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(username, password);
		}
	}

	private String username;
	private String port;
	private String password;
	private boolean authReuired;
	
	
	public boolean isAuthReuired() {
		return authReuired;
	}

	public void setAuthReuired(boolean authReuired) {
		this.authReuired = authReuired;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	
}